package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.iot.vo.RoomDiviceAssociationVo;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.nonIntelligentDevice.vo.DepRoomDevicePriceVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.nonIntelligentDevice.vo.RoomDevicePriceAssociationVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.nonIntelligentDevice.vo.RoomNameVO;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomDevicePriceAssociationMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomDevicePriceAssociation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @BelongsProject: lease-module-rooms
 * @BelongsPackage: com.xiaoshuidi.cloud.module.rooms.repository
 * @Author: James
 * @CreateTime: 2023-06-07  14:55
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class IotRoomDevicePriceAssociationRepository extends ServiceImpl<RoomDevicePriceAssociationMapper, RoomDevicePriceAssociation> {
    @Autowired
    private RoomDevicePriceAssociationMapper roomDevicePriceAssociationMapper;


    public int removeRoomDevice(Long roomDeviceId) {
        QueryWrapper<RoomDevicePriceAssociation> wrapper = new QueryWrapper();
        wrapper.eq("device_price_id", roomDeviceId);
        return roomDevicePriceAssociationMapper.delete(wrapper);
    }

    public List<RoomDevicePriceAssociation> getNotInRoom(Long priceId, Integer type) {
        QueryWrapper<RoomDevicePriceAssociation> wrapper = new QueryWrapper();
        wrapper.notIn("rsdpa.device_price_id", priceId);
        wrapper.eq("rsdp.type", type);
        wrapper.eq("rsdpa.deleted", "0");
        return roomDevicePriceAssociationMapper.getNotInRoom(wrapper);
    }

    public List<Long> getNotRoomIdPay() {
        return roomDevicePriceAssociationMapper.getNotRoomIdPay();
    }

    public List<RoomDevicePriceAssociation> getDeviceAssociationId(Long roomId) {
        QueryWrapper<RoomDevicePriceAssociation> wrapper = new QueryWrapper();
        wrapper.eq("room_id", roomId);
        return roomDevicePriceAssociationMapper.selectList(wrapper);
    }

    public int updateDeviceAssociation(Long oldId, Long newId) {
        QueryWrapper<RoomDevicePriceAssociation> wrapper = new QueryWrapper();
        wrapper.eq("device_price_id", oldId);
        RoomDevicePriceAssociation priceAssociation = new RoomDevicePriceAssociation();
        priceAssociation.setDevicePriceId(newId);
        return roomDevicePriceAssociationMapper.update(priceAssociation, wrapper);
    }


    public CommonResult<String> setPriceToRoom(RoomDevicePriceAssociationVO roomDevicePriceAssociationVO) {
        // todo @黄光辉 一次性应用一千个房间，你这里要发起2001次数据库请求; 优化掉
        Long devicePriceId = roomDevicePriceAssociationVO.getDevicePriceId();
        Long deptId = roomDevicePriceAssociationVO.getDeptId();
        List<Long> list = roomDevicePriceAssociationVO.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Long roomId : list) {
                QueryWrapper<RoomDevicePriceAssociation> qw = new QueryWrapper<>();
                qw.eq("room_id", roomId);
                RoomDevicePriceAssociation priceAssociation = roomDevicePriceAssociationMapper.selectOne(qw);
                if (ObjectUtil.isNotEmpty(priceAssociation)) {
                    List<RoomNameVO> roomName = roomDevicePriceAssociationMapper.findRoomName(roomId);
                    RoomNameVO roomNameVO = roomName.get(0);
                    if (roomNameVO.getIsConcentrated()) {
                        String finalName = roomNameVO.getArea() + "小区" + roomNameVO.getBuildingNo() + "栋" + roomNameVO.getHouseNo();
                        return CommonResult.error(1, finalName + "该房间设备已应用付费价格,请先取消！");
                    } else {
                        String finalName = roomNameVO.getArea() + roomNameVO.getBuildingNo() + "栋" + roomNameVO.getUnitNo() + "单元" + roomNameVO.getHouseNo();
                        return CommonResult.error(1, finalName + "该房间设备已应用付费价格,请先取消！");
                    }
                }
            }
        }

        QueryWrapper<RoomDevicePriceAssociation> qw = new QueryWrapper<>();
        qw.eq("device_price_id", devicePriceId);
        roomDevicePriceAssociationMapper.delete(qw);
        if (CollectionUtil.isNotEmpty(list)) {
            List<RoomDevicePriceAssociation> associationList = new ArrayList<>();
            for (Long roomId : list) {
                RoomDevicePriceAssociation roomDevicePriceAssociation = new RoomDevicePriceAssociation();
                roomDevicePriceAssociation.setDevicePriceId(devicePriceId);
                roomDevicePriceAssociation.setRoomId(roomId);
                roomDevicePriceAssociation.setDeptId(deptId);
                associationList.add(roomDevicePriceAssociation);
            }
            this.saveBatch(associationList);
        }
        return CommonResult.success("该付费价格已应用到房间设备");
    }

    public CommonResult<List<DepRoomDevicePriceVO>> findAllRoomId(long devicePriceId) {
        List<DepRoomDevicePriceVO> voList = new ArrayList<>();
        QueryWrapper<RoomDevicePriceAssociation> qw = new QueryWrapper<>();
        qw.eq("rsdpa.device_price_id", devicePriceId)
                .eq("rsdpa.deleted", false);
        List<RoomDiviceAssociationVo> list = roomDevicePriceAssociationMapper.getRoomDeviceAssociationList(qw);
        Map<Long, DepRoomDevicePriceVO> hashMap = new HashMap<>();
        for (RoomDiviceAssociationVo roomDevicePriceAssociation : list) {
            Long roomHouseId = roomDevicePriceAssociation.getRoomId();
            //Long deptId = roomDevicePriceAssociation.getDeptId();
            Long apartment = roomDevicePriceAssociation.getApartmentId();
            if (hashMap.containsKey(apartment)) {
                hashMap.get(apartment).getList().add(roomHouseId);
                hashMap.get(apartment).setApartmentId(apartment);
                hashMap.put(apartment, hashMap.get(apartment));
            } else {
                DepRoomDevicePriceVO depRoomDevicePriceVO = new DepRoomDevicePriceVO();
                depRoomDevicePriceVO.setApartmentId(apartment);
                List<Long> longList = new ArrayList<>();
                longList.add(roomHouseId);
                depRoomDevicePriceVO.setList(longList);
                hashMap.put(apartment, depRoomDevicePriceVO);
            }
        }
        Set<Map.Entry<Long, DepRoomDevicePriceVO>> entries = hashMap.entrySet();
        for (Map.Entry<Long, DepRoomDevicePriceVO> entry : entries) {
            DepRoomDevicePriceVO value = entry.getValue();
            voList.add(value);
        }

        return CommonResult.success(voList);
    }

    public void deleteRoomDevice(Long id, Long apartmentId) {
        roomDevicePriceAssociationMapper.deleteRoomDevice(id,apartmentId);
    }
}
